package com.adminx.dao.impl;

import com.adminx.dao.NewsDao;
import com.adminx.po.News;
import com.javaweb.common.NullableTool;
import com.javaweb.common.StringTool;

import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl extends BaseDao implements NewsDao {
    @Override
    public int getNewsCount(String keywords, Integer newsListCategoryId) {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM t_news WHERE ") ;

       if (! StringTool.isEmpty(keywords)) {
            sql.append(" title LIKE ? AND ") ;
        }
        if (! NullableTool.isNull(newsListCategoryId)) {
            sql.append(" categoryId = ? ") ;
        } else {
            sql.append(" 1=1");
        }
        this.doQuery(sql.toString(), keywords == null ? null: "%"+keywords+"%",
                                    newsListCategoryId);
        try {
            if (this.rs.next()) {
                return this.rs.getInt(1);
            }
        } catch (Exception e) {
           e.printStackTrace();
        } finally {
            this.close();
        }
        return 0;
    }

    @Override
    public List<News> findNewsListByPage(String keywords,Integer newsListCategoryId, Integer startRows, Integer pageSize) {
        List<News> newsList = null ;
        News news = null ;
        String sql = "SELECT n.newsId, n.title, n.publishTime, n.publishStatus,n.content, c.categoryName FROM t_news as n LEFT JOIN t_category as c " +
                "ON n.categoryId = c.categoryId WHERE n.title LIKE ? ORDER BY n.publishTime desc LIMIT ?, ?" ;
        if (keywords == null) {
            keywords = "" ;
        }
        this.doQuery(sql,  "%"+keywords+"%", startRows, pageSize);
        try {
            while (this.rs.next()) {
                if (newsList == null) {
                    newsList = new ArrayList<>();
                }
                news = new News() ;
                news.setNewsId(rs.getInt(1)) ;
                news.setTitle(rs.getString(2));
                news.setPublishTime(rs.getDate(3));
                news.setPublishStatus(rs.getString(4));
                news.setContent(rs.getString(5));
                news.setCategoryName(rs.getString(6));
                newsList.add(news) ;
            }
            return newsList ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public News getNewsByNewsId(Integer newsId) {
        return null;
    }

    @Override
    public int addNews(News news) {
        String sql = "INSERT INTO t_news (newsId, title, contentTitle," +
                "content, contentAbstract, keywords, author, publishTime, clicks, categoryId, userId) VALUES " +
                "(?,?,?,?,?,?,?,?,?,?,?)" ;
        return this.doUpate(sql, news.getNewsId(), news.getTitle(), news.getContentTitle(),
                        news.getContent(), news.getContentAbstract(), news.getKeywords(), news.getAuthor(),
                news.getPublishTime(), news.getClicks(), news.getCategoryId(), news.getUserId()) ;
    }

    @Override
    public int updateNews(News news) {
        return 0;
    }

    @Override
    public int setNewsPublishStatus(News news) {
        return 0;
    }

    @Override
    public int delNews(Integer newsId) {
        return 0;
    }
}
